package net.FamilyTree.controller;

import net.FamilyTree.entity.Event;
import net.FamilyTree.service.EventService;
import net.FamilyTree.util.ResultVoUtil;
import net.FamilyTree.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/event")
public class EventController {

    @Autowired
    private EventService eventService;

    // 保存事件
    @PostMapping("/save")
    public ResultVo save(@RequestBody Event event) {
        Boolean add = this.eventService.add(event);
        if (!add) {
            return ResultVoUtil.fail();
        }
        return ResultVoUtil.success(null);
    }

    // 根据人员ID、页码和每页大小获取事件列表
    @GetMapping("/list/{genealogyId}/{page}/{size}")
    public ResultVo list(
            @PathVariable("genealogyId") Integer genealogyId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size) {
        return ResultVoUtil.success(this.eventService.list(genealogyId, page, size));
    }

    // 根据事件ID查找事件
    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {
        Event event = this.eventService.getById(id);
        return ResultVoUtil.success(event);
    }

    // 更新事件
    @PutMapping("/update")
    public ResultVo update(@RequestBody Event event) {
        Boolean update = this.eventService.update(event);
        if (!update) {
            return ResultVoUtil.fail();
        }
        return ResultVoUtil.success(null);
    }

    // 根据事件ID删除事件
    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id) {
        Boolean delete = this.eventService.delete(id);
        if (!delete) {
            return ResultVoUtil.fail();
        }
        return ResultVoUtil.success(null);
    }
}
